package com.sunny.ihealth.ui.view;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.text.InputFilter;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.sunny.ihealth.R;


/**
 * 各种对话框
 * Created by Sunny  An on 2015/10/10.
 */
public class SomeDialogs {

    public interface SubmitListener {
        public void onSubmit(String text);
    }


    /**
     * 输入文本的对话框
     * @param context 上下文
     * @param listener 获取输入文本的回调函数
     * @param text 编辑框内容
     * @param title 标题
     * @param hint 编辑框提示
     * @param okText  确认按钮
     * @param cancelText 取消按钮
     * @param maxLength 最大长度
     */
    public static void inputDialog(Context context, final SubmitListener listener, String text, String title, String hint, String okText, String cancelText, int maxLength) {
        View v = LayoutInflater.from(context).inflate(R.layout.dialog_input_text, null);
        final EditText editText = (EditText) v.findViewById(R.id.editText_dialog_text);
        if (text != null) {
            editText.setText(text);
        }
        editText.setSingleLine(true);
        editText.setHint(hint);
        InputFilter[] filter = new InputFilter[1];
        filter[0] = new InputFilter.LengthFilter(maxLength);
        editText.setFilters(filter);
        new AlertDialog.Builder(context)
                .setTitle(title)
                .setView(v)
                .setPositiveButton(okText, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listener.onSubmit(editText.getText().toString());
                    }
                })
                .setNegativeButton(cancelText, null)
                .create()
                .show();
    }


    /**
     * 进度对话框
     * @param context
     * @param text
     * @return
     */
    public static ProgressDialog unCancelableProgressDialog(Context context, String text) {
        ProgressDialog dialog = new ProgressDialog(context);
        dialog.setMessage(text);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        return dialog;
    }

    /**
     * 简单的提示对话框
     * @param context
     * @param text
     * @return
     */
    public static AlertDialog simpleAlertDialog(Context context, String text) {
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setMessage(text);
        builder.setPositiveButton("确定", null);
        return builder.create();
    }

}
